//
//  GameScene.m
//  FairyGame
//
//  Created by 李 世宸 on 2009/7/30.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "AliceScene.h"
#import "MenuScene.h"

enum {
	Action_Tag,
	ActionIdel_Tag,
	ActionWalk_Tag,
	ActionJump_Up_Tag,
	ActionJump_Down_Tag,	
};

@implementation AliceScene
- (id) init 
{
    self = [super init];
    if (self != nil) 
	{
		//背景
        //Sprite * bg = [Sprite spriteWithFile:@"bg2.png"];
        //[bg setPosition:ccp(240, 160)];
		
		//連結背景
        //[self addChild:bg z:0];
		
		//連結Layer
		[self addChild:[AliceActorLayer node] z:1];
    }
    return self;
}
@end

@implementation AliceBtnLayer
- (id) init 
{
    self = [super init];
    if (self != nil) 
	{
        //isTouchEnabled = YES;
		
		//按鈕Layer
		MenuItemImage *item1 = [MenuItemImage itemFromNormalImage:@"ten_btn_up01.png" selectedImage:@"ten_btn_up02.png" target:self selector:@selector(FaceUp:)];
		MenuItemImage *item2 = [MenuItemImage itemFromNormalImage:@"ten_btn_right01.png" selectedImage:@"ten_btn_right02.png" target:self selector:@selector(FaceRight:)];
		MenuItemImage *item3 = [MenuItemImage itemFromNormalImage:@"ten_btn_left01.png" selectedImage:@"ten_btn_left02.png" target:self selector:@selector(FaceLeft:)];
		MenuItemImage *item4 = [MenuItemImage itemFromNormalImage:@"ten_btn_down01.png" selectedImage:@"ten_btn_down02.png" target:self selector:@selector(FaceDown:)];
		MenuItemImage *item5 = [MenuItemImage itemFromNormalImage:@"ten_btn_center.png" selectedImage:@"ten_btn_center.png" target:self selector:@selector(FaceCenter:)];
		
		Menu *menu = [Menu menuWithItems:item1, item2, item3, item4, item5, nil];
		menu.position = CGPointZero;
		item1.position = ccp(60+37,70/2+68);
		item2.position = ccp(60+67,70/2+38);
		item3.position = ccp(60+7,70/2+38);
		item4.position = ccp(60+37,70/2+8);
		item5.position = ccp(60+37,70/2+38);
		[self addChild: menu z:1];	
		
    }
    return self;
}

-(void) FaceUp: (id)sender 
{
}

-(void) FaceRight: (id)sender 
{
	AliceActorLayer *layer = [AliceActorLayer node];
	//CocosNode *s = [layer getAlice];
	[layer ActorWalk];
}

-(void) FaceLeft: (id)sender 
{
}

-(void) FaceDown: (id)sender 
{
}

-(void) FaceCenter: (id)sender 
{
}

- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    MenuScene * ms = [MenuScene node];
    [[Director sharedDirector] replaceScene:ms];
    return kEventHandled;
	
	/*
	- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
	{
		UITouch *touch = [touches anyObject];
		
		CGPoint location = [touch locationInView: [touch view]];
		CGPoint convertedLocation = [[Director sharedDirector] convertCoordinate:location];
		
		CocosNode *s = [self getChildByTag:kTagSprite];
		[s stopAllActions];
		[s runAction: [MoveTo actionWithDuration:1 position:ccp(convertedLocation.x, convertedLocation.y)]];
		float o = convertedLocation.x - [s position].x;
		float a = convertedLocation.y - [s position].y;
		float at = (float) CC_RADIANS_TO_DEGREES( atanf( o/a) );
		
		if( a < 0 ) {
			if(  o < 0 )
				at = 180 + abs(at);
			else
				at = 180 - abs(at);	
		}
		
		[s runAction: [RotateTo actionWithDuration:1 angle: at]];
		
		return kEventHandled;
	}
	 */
}
@end

@implementation AliceActorLayer
- (id) init 
{
    self = [super init];
    if (self != nil) 
	{
		Texture2D *tex = [ [Texture2D alloc] initWithImage: [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"alice_idel_01.png" ofType:nil] ] ];
		Alice = [[Sprite spriteWithTexture:tex] retain];
		[tex release];
		
		[self addChild: Alice z:1 tag:Action_Tag];
	    //抓螢幕Size
		CGSize s = [[Director sharedDirector] winSize];
		//設定位置
		[Alice setPosition: ccp(60, s.height/3)];
		
		[self centerSprites];
		[self ActorIdel];
		
		[self addChild:[AliceBtnLayer node] z:1];
    }
    return self;
}

-(void) centerSprites
{
	CGSize s = [[Director sharedDirector] winSize];	
	[Alice setPosition: ccp(s.width/3, s.height/2)];
}

-(void) ActorWalk
{
	//[Alice stopAllActions];
	
	Animation* animationwalk = [Animation animationWithName:@"walk" delay:0.2f];
	for( int i=1;i<5;i++)
		[animationwalk addFrameWithFilename: [NSString stringWithFormat:@"alice_walk_%02d.png", i]];
	
	id actionwalk = [Animate actionWithAnimation: animationwalk];
	//[actionwalk setTag: ActionWalk_Tag];
	
	//重複..
	[Alice runAction:actionwalk];
	//[Alice runAction:[RepeatForever actionWithAction:actionwalk]];
}

-(void) ActorIdel
{
	//[Alice stopActionByTag:ActionWalk_Tag];
	//[Alice stopAllActions];
	
	Animation* animationidel = [Animation animationWithName:@"idel" delay:0.2f];
	for( int i=1;i<7;i++)
		[animationidel addFrameWithFilename: [NSString stringWithFormat:@"alice_idel_%02d.png", i]];
	
	id actionidel = [Animate actionWithAnimation: animationidel];
	//[Alice runAction:[RepeatForever actionWithAction:actionidel]];
	[Alice runAction:actionidel];
}

-(void) onEnter
{
	[super onEnter];
}

-(CocosNode*) getAlice
{
	return Alice;
}

-(void) dealloc
{	
	[Alice release];
	[super dealloc];
}
@end






